package demo;

import java.lang.reflect.Field;

public class AccessPeronFields {
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            Class cls=Class.forName("demo.Person");
            Object person=cls.newInstance();
            Field name=cls.getDeclaredField("name");
            name.setAccessible(true);

            System.out.println("赋值前的name："+name.get(person));
            name.set(person,"New Person");

            System.out.println("赋值后的name："+name.get(person));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}
